package com.epoint.mobile.demo.drag;

import java.io.IOException;
import java.io.Serializable;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.GridView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.tools.IOHelp;

/**
 * This activity presents a screen with a grid on which images can be added and moved around.
 * It also defines areas on the screen where the dragged views can be dropped. Feedback is
 * provided to the user as the objects are dragged over these drop zones.
 *
 * <p> Like the DragActivity in the previous version of the DragView example application, the
 * code here is derived from the Android Launcher code.
 * 
 * <p> The original Launcher code required a long click (press) to initiate a drag-drop sequence.
 * If you want to see that behavior, set the variable mLongClickStartsDrag to true.
 * It is set to false below, which means that any touch event starts a drag-drop.
 * 
 */

public class DragActivity extends Activity implements 
View.OnLongClickListener, 
View.OnTouchListener ,Serializable
{
	private DragController mDragController;   // Object that handles a drag-drop sequence. It intersacts with DragSource and DropTarget objects.
	private DragLayer mDragLayer;             // The ViewGroup within which an object can be dragged.
	private boolean mLongClickStartsDrag = false;   // If true, it takes a long click to start the drag operation.
	public static final boolean Debugging = false;   // Use this to see extra toast messages.
	private DeleteZone mDeleteZone;
	public void addNewImageToScreen (FrameLayout imageHolder,int img)
	{
		if (imageHolder != null) {
			FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams (LayoutParams.FILL_PARENT, 
					LayoutParams.FILL_PARENT, 
					Gravity.CENTER);

			ImageCell newView = new ImageCell (this);
			newView.setImageResource (img);
			imageHolder.addView (newView, lp);
			newView.mEmpty = false;
			newView.mCellNumber = -1;
			newView.setOnLongClickListener(this);
			newView.setOnTouchListener (this);
		}
	}
	GridView gridView;
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.demo);
		gridView = (GridView) findViewById(R.id.image_grid_view);
		gridView.setAdapter (new ImageCellAdapter(this));
		mDragController = new DragController(this);
		mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
		mDragLayer.setDragController (mDragController);
		mDragLayer.setGridView (gridView);
		mDragController.setDragListener (mDragLayer);

		addNewImageToScreen((FrameLayout) findViewById (R.id.image_source_frame),R.drawable.ts);
		addNewImageToScreen((FrameLayout) findViewById (R.id.image_source_frame2),R.drawable.ts2);
		addNewImageToScreen((FrameLayout) findViewById (R.id.image_source_frame3),R.drawable.ts3);
		addNewImageToScreen((FrameLayout) findViewById (R.id.image_source_frame4),R.drawable.ts4);
		addNewImageToScreen((FrameLayout) findViewById (R.id.image_source_frame5),R.drawable.ts5);
		addNewImageToScreen((FrameLayout) findViewById (R.id.line),R.drawable.line3);
		addNewImageToScreen((FrameLayout) findViewById (R.id.lineup),R.drawable.lineup);
		addNewImageToScreen((FrameLayout) findViewById (R.id.lineleft),R.drawable.lineleft);
		addNewImageToScreen((FrameLayout) findViewById (R.id.lineright),R.drawable.line_right);
		mDeleteZone = (DeleteZone) findViewById (R.id.delete_zone_view);
	}

	

	public boolean onLongClick(View v) 
	{
		if (mLongClickStartsDrag) {
			if (!v.isInTouchMode()) {
				return false;
			}
			return startDrag (v,0);
		}
		return false;
	}

	public boolean onTouch (View v, MotionEvent ev) 
	{
		if (mLongClickStartsDrag) return false;
		boolean handledHere = false;
		final int action = ev.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			handledHere = startDrag (v,0);
		}
		return handledHere;
	}   

	public boolean startDrag (View v,int flag)
	{
		DragSource dragSource = (DragSource) v;
		mDragController.startDrag (v, dragSource, dragSource, DragController.DRAG_ACTION_COPY,flag);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
			byte[] b = IOHelp.obj2bytes(this);
			System.out.println(b.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


} 
